/* eslint-disable */
import UedService from '../../service/ude'
export default {
  data() {
    return {
      isLoadingBtn: false,
      historyItems: [],
      items: [],
      name: '',
      menuItems: []
    }
  },
  mounted() {
    this.getFiles()

    let uedBackHistoryItems = localStorage.getItem('uedBackHistoryItems')
    if (uedBackHistoryItems) {
      this.historyItems = JSON.parse(uedBackHistoryItems)
    }
  },
  methods: {
    getPath() {
      if (this.menuItems.length > 0) {
        return this.menuItems[this.menuItems.length - 1].fullPath
      }
      return ''
    },

    // 搜索按钮
    search(item) {
      if (item) {
        this.name = item
      }
      if (this.name != '') {
        let index = this.historyItems.indexOf(this.name)
        if (index > -1) {
          this.historyItems.splice(index, 1)
          this.historyItems.unshift(this.name)
        } else {
          this.historyItems.unshift(this.name)
          if (this.historyItems.length > 5) {
            this.historyItems.splice(this.historyItems.length - 1, 1)
          }
        }
        localStorage.setItem('uedBackHistoryItems', JSON.stringify(this.historyItems))
      }
      this.getFiles()
    },

    getFileListBySearch(item) {
      let param = {
        name: this.name
      }
      if (item) {
        param.fullPath = item.fullPath
      } else {
        param.fullPath = this.getPath()
      }
      UedService.getFiles(param).then(res => {
        if (res.success) {
          if (res.data && res.data.length > 0) {
            res.data.forEach(item => {
              if (item.isDirectory) {
                this.getFileListBySearch(item)
              }
            })
            let arry = res.data.filter(item => item.fileName.indexOf(this.name) > -1)
            Array.prototype.push.apply(this.items, arry)

            this.$set(this.items, this.items)
          }
        }
      })
    },

    getFiles(item) {
      let param = {
        name: this.name
      }
      if (item) {
        param.fullPath = item.fullPath
      } else {
        param.fullPath = this.getPath()
      }
      this.isLoadingBtn = true
      UedService.getFiles(param).then(res => {
        if (res.success) {
          let hasIndexHtml = false;
          if (res.data && res.data.length > 0) {
            res.data.forEach(file => {
              if (file.fileName == 'index.html' || file.fileName == 'index.htm') {
                hasIndexHtml = true
              }
            })
          }
          if (!hasIndexHtml) {
            if (item) {
              this.menuItems.push(item)
            }
            this.items = res.data;
          } else {
            // window.open('http://10.0.54.9:9080' + item.fullPath, '_blank');
            // var tempwindow = window.open('_blank')
            // tempwindow.location = 'http://10.0.54.9:9080' + item.fullPath
            setTimeout(() => {
              window.open(item.fullPath, '_blank');
            }, 0);
          }
        }
      }).finally(() => {
        this.isLoadingBtn = false
      })
    },
    downLoad(item) {
      window.open(item.fullPath)
    },

    rowClick(item) {
      if (item.isDirectory) {
        this.name = ''
        this.getFiles(item)
      } else if (!item.isDownLoad) {
        window.open(item.fullPath)
      }
    },

    clickMenuItem(item, index) {
      this.name = ''
      if (item) {
        this.menuItems = this.menuItems.filter((item, idx) => idx < index)
      } else {
        this.menuItems = []
      }
      this.getFiles(item)
    }
  },
  filters: {
    formatDate(m, fmt) {
      let date = new Date(m)
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + ''
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (('00' + str).substr(str.length)))
        }
      }
      return fmt
    }
  }
}